#!/bin/bash
###
 # @Author: dvlproad dvlproad@163.com
 # @Date: 2023-07-31 19:46:02
 # @LastEditors: dvlproad dvlproad@163.com
 # @LastEditTime: 2023-08-02 22:33:45
 # @FilePath: /AutoPackage-CommitInfo/bulidScriptCommon/update_package_version_description.sh
 # @Description: 更新安装包的中英文信息,方便后面直接取值
### 
#sh update_package_version_description.sh -envJsonF "${Environment_FILE_PATH}"
#sh update_package_version_description.sh -envJsonF "../example_packing_info/app_info.json"


# shell 参数具名化
show_usage="args: [-envJsonF]\
                                  [--environment-json-file=]"
                                  
while [ -n "$1" ]
do
        case "$1" in
                -envJsonF|--environment-json-file=) Environment_FILE_PATH=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done



# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}/..
# bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}
#echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"

qscript_path_get_filepath="${bulidScriptCommon_dir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"



JQ_EXEC=`which jq`

if [ ! -f "$Environment_FILE_PATH" ];then
    echo "❌Error:${Environment_FILE_PATH}不存在，请检查"
fi

# 1、版本的英文信息
packageVersion=$(cat $Environment_FILE_PATH | ${JQ_EXEC} .version | sed 's/\"//g')
packageBuildNumber=$(cat $Environment_FILE_PATH | ${JQ_EXEC} .buildNumber | sed 's/\"//g')
packageCreateTime=$(cat $Environment_FILE_PATH | ${JQ_EXEC} .package_create_time | sed 's/\"//g')
#echo "packageVersion=$packageVersion,packageBuildNumber=$packageBuildNumber,packageCreateTime=$packageCreateTime"
if [ ! -n "${packageVersion}" ]; then
    echo "❌Error:${Environment_FILE_PATH}解析失败,文件中的内容格式出错，不是标准json"
fi

source ${bulidScriptCommon_dir_Absolute}/a_source_env.sh -appInfoF ${Environment_FILE_PATH}
NewPackageVersionDes="[${BRANCH}]${PackageTargetType}_${PackageNetworkType}_${PlatformType}V${packageVersion}(${packageBuildNumber})"
sh ${qbase_update_json_file_singleString_script_path} -jsonF "${ENVIRONMENT_FILE_PATH}" -k "package_des.english" -v "${NewPackageVersionDes}"

# 2、版本的中文信息
PackageTargetNetworkDes=$(cat $Environment_FILE_PATH | ${JQ_EXEC} .package_result | ${JQ_EXEC} .package_description | sed 's/\"//g')
#echo "PackageTargetNetworkDes=${PackageTargetNetworkDes}"
if [ "${PackageTargetType}" == "dev" ]; then
    PgyerUploadUrlType=$(cat $Environment_FILE_PATH | ${JQ_EXEC} .package_result | ${JQ_EXEC} .pgyerUploadUrlType | sed 's/\"//g')
    if [ "${PgyerUploadUrlType}" == "toDownUrl" ]; then
        PackagePgyerPublishStateDes="会直接发布的"
    elif [ "${PgyerUploadUrlType}" == "toUploadUrl" ]; then
        PackagePgyerPublishStateDes="待发布的"
    elif [ "${PgyerUploadUrlType}" == "toNoneUrl" ]; then
        PackagePgyerPublishStateDes="不会发布的"
    fi
    PackageTargetNetworkDes="${PackagePgyerPublishStateDes}${PlatformType}${PackageTargetNetworkDes}"
fi
echo "==============PackageTargetNetworkDes=${PackageTargetNetworkDes}"
sh ${qbase_update_json_file_singleString_script_path} -jsonF "${ENVIRONMENT_FILE_PATH}" -k "package_des.chinese" -v "${PackageTargetNetworkDes}"



PackageDesEnglish=$(cat ${ENVIRONMENT_FILE_PATH} | ${JQ_EXEC} -r '.package_des.english')
echo "==============PackageDesEnglish=${PackageDesEnglish}"
#if [ -n "${PackageDesEnglish}" ]; then # 当字符串 str1 的长度大于 0（非空）时为真
#    echo "非空"
#fi
